<?php
class model_product extends CI_Model
{
    function __construct()
	{
	    parent::__construct();	
	}
	
	function save()
	{
		$post = $this->input->post();
		$post['created'] = time();
		
		$root_path = $_SERVER['DOCUMENT_ROOT'];
		$config['upload_path'] = $root_path.'/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '100';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$this->load->library('upload',$config);
		
		if(! $this->upload->do_upload('product_image'))
		{
			$error = array('error'=>$this->upload->display_errors());
			var_dump($error);
		}else{
			$data = array('upload_data'=>$this->upload->data());
			$post['product_image'] = '/upload/'.$data['upload_data']['orig_name'];
		}
		
		$this->db->insert('product',$post);
		
	}
	
	function update()
	{
		$post = $this->input->post();
		$pid = intval($post['pid']);
		$post['created'] = time();
		if(!empty($_FILES['product_image'])){
				$root_path = $_SERVER['DOCUMENT_ROOT'];
				$config['upload_path'] = $root_path.'/upload/';
				$config['allowed_types'] = 'gif|jpg|png';
				$config['encrypt_name'] = true;
				$config['max_size'] = '100';
				$config['max_width'] = '1024';
				$config['max_height'] = '768';
				$this->load->library('upload',$config);
				
				if(! $this->upload->do_upload('product_image'))
				{
					unset($post['product_image']);
				}else{
					$data = array('upload_data'=>$this->upload->data());
					$post['product_image'] = '/upload/'.$data['upload_data']['file_name'];
				}
		}else{
			unset($post['product_image']);
		}
			
		
		unset($post['pid']);
		$this->db->where('pid',$pid)->update('product',$post);
	}
	
	function cat_save()
	{
	    $post = $this->input->post();
		
		$this->db->insert('product_cat',$post);
	}
	
	function cat_update()
	{
	    $post = $this->input->post();
		$id = intval($post['id']);
		unset($post['id']);
		
		$this->db->where('id',$id)->update('product_cat',$post);
	}
}













